#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define fileNum 390
#define dataNum 29
int main()
{
	FILE *fpopen = NULL,*fpwrite = NULL,*fpopen2 = NULL;
	int ID=0,GroupID=0,TypeID=0;
	float data[dataNum]; 
	int column_number=0;
	int a=0;
	char c[1000];
	char fname[30];
	char fname2[30];
	char fname3[30];
	
	for (int i=0;i<dataNum;i++)
	{
		data[i]=0.0;
	}
	
	for (int j=1;j<=fileNum;j++)
	{

		sprintf(fname,"J:\paper\imbalance2023\filling\cavity\imbalance\innermorethanouter\inner\%d.csv",j);
	//	sprintf(fname,"out_000146_primary.csv");
		sprintf(fname2,"J:\paper\imbalance2023\filling\cavity\imbalance\innermorethanouter\outer\out_%06d_wholeouter.csv",j);	
	//	sprintf(fname2,"out_000001_primary_new.csv");
		sprintf(fname3,"out_%06d_whole.csv",j);
		if((fpopen=fopen(fname,"r"))==NULL)
		{
			printf("11Can not open file,%s\n",fname);
		//	system("pause");
			exit(1);
		}
		if((fpopen2=fopen(fname2,"r"))==NULL)
		{
			printf("Can not open file,%s\n",fname);
			exit(1);
		}
		if((fpwrite=fopen(fname3,"w"))==NULL)
		{
			printf("Can not open file, %s\n",fname2);
			exit(1);
		}
		//计算行数
		while((a=fgetc(fpopen))!=EOF)
		{
			if(a=='\n')
				column_number++;
		}
		rewind(fpopen);
		printf("%d\n",column_number);


		// 写入第一个文件
	    fscanf(fpopen,"%[^\n]", c);
	
	    for(int i=0;i<strlen(c);i++)
		     fputc(c[i],fpwrite);
	    fprintf(fpwrite,"\n");


	     while (!feof(fpopen))
	      {
 
	        fscanf(fpopen,"%d,%d,%d",&ID,&GroupID,&TypeID);
	        for (int i=0;i<dataNum;i++)
	        {
		       fscanf(fpopen,",%f",&data[i]);
	        }
	 
	        if(feof(fpopen))
		       break;
	

	        fprintf(fpwrite,"%d,%d,%d",ID,GroupID,TypeID);
            //	printf("%d,%d,%d",ID,GroupID,TypeID);
	        for (int i=0;i<dataNum;i++)
	        {
	     	fprintf(fpwrite,",%f",data[i]);
           //		printf(",%f",data[i]);
	        }

	        fprintf(fpwrite,"\n");
          //	printf("\n");
       	}
	
	
	    //写入第二个文件
	   fscanf(fpopen2,"%[^\n]", c);
	   while (!feof(fpopen2))
	  {
 
	     fscanf(fpopen2,"%d,%d,%d",&ID,&GroupID,&TypeID);
	      for (int i=0;i<dataNum;i++)
	      {
	 	 fscanf(fpopen2,",%f",&data[i]);
	    }
		  if(feof(fpopen2))
			  break;

	    ID=column_number+ID;
	
	   fprintf(fpwrite,"%d,%d,%d",ID,GroupID,TypeID);
	
   //	printf("%d,%d,%d",ID,GroupID,TypeID);
	   for (int i=0;i<dataNum;i++)
	   {
	  	  fprintf(fpwrite,",%f",data[i]);
    //		printf(",%f",data[i]);
	    }

	     fprintf(fpwrite,"\n");
     //	printf("\n");
	  }

	    fclose(fpopen);
	    fclose(fpopen2); 
    	fclose(fpwrite);       
	    fpopen = NULL;
	    fpopen2 = NULL;
	   fpwrite = NULL;


	}


	return 0;
}